package com.example.xiaoxueqixiangmu.jiemian;

/**
 * Create By Visen
 * Date: 2021/8/31
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.xiaoxueqixiangmu.R;

import java.util.ArrayList;

/**
 * Create By Visen
 * Date: 2021/6/23
 */

public class ChooseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<String> dataArray;
    private Context context;
    private methodObject methodObject;

    public interface methodObject {
        void method(View view, int position);
    }

    public void setMethodObject(methodObject methodObject){
        this.methodObject = methodObject;
    }

    public ChooseAdapter(Context context, ArrayList<String> dataArray){
        this.context = context;
        this.dataArray = dataArray;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View item = layoutInflater.inflate(R.layout.item_choose_train, parent, false);
        ViewItem viewItem = new ViewItem(item);
        return viewItem;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if( holder instanceof ViewItem ){
            String str = dataArray.get(position);
            ((ViewItem) holder).city.setText(str);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    methodObject.method(v,position);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return dataArray.size();
    }

    public class ViewItem extends RecyclerView.ViewHolder{
        private TextView city;
        public ViewItem(@NonNull View itemView) {
            super(itemView);
            city = itemView.findViewById(R.id.city);
        }
    }

}
